package kuzmenkov.applet;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Logger;

/**
 * 
 * @author Michael Kuzmenkov
 * 
 */
public class ImageDlgMouseListener extends MouseAdapter {

	private Logger log = Logger.getLogger(this.getClass().getPackage().getName());

	/**
	 * The distances between a left top corner of an originator and
	 * the location of the mouse cursor
	 */
	private int dX = 0, dY = 0;

	@Override
	public void mouseClicked(MouseEvent e) {
		Component component = e.getComponent();
		if (component == null) {
			log.warning("The originator of the event is not a Component");
			return;
		}

		component.setVisible(false);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		dX = e.getX();
		dY = e.getY();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		Component comp = e.getComponent();
		if (comp == null) {
			log.warning("The originator of the event is not a Component");
			return;
		}

		int newX = e.getXOnScreen() - dX;
		int newY = e.getYOnScreen() - dY;

		comp.setLocation(newX, newY);
	}

}
